package com.example.rbacmanage.controller.app;

import com.alibaba.druid.util.StringUtils;
import com.example.rbacmanage.common.Result;
import com.example.rbacmanage.entity.system.DictData;
import com.example.rbacmanage.entity.system.DictType;
import com.example.rbacmanage.exception.MyException;
import com.example.rbacmanage.service.system.DictDataService;
import com.example.rbacmanage.service.system.DictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/app/dictdata")
public class DictDataAppController {
    @Autowired
    private DictDataService dictDataService;

    @Autowired
    private DictTypeService dictTypeService;

    @GetMapping("/getDictDataByDictType")
    public Result list(String dictTypeTypeSign) throws MyException {
        if (StringUtils.isEmpty(dictTypeTypeSign)){
            throw new MyException("参数不能为空",HttpStatus.INTERNAL_SERVER_ERROR.value());
        }
        DictType dictType = new DictType();
        dictType.setTypeSign(dictTypeTypeSign);
        List<DictType> list = dictTypeService.getAll(dictType);
        DictData dictData = new DictData();
        if (!list.isEmpty()){
            dictData.setDictTypeId(list.get(0).getId());
        }
        return Result.success(dictDataService.getAll(dictData));
    }
}
